package com.iflytek.cosmo.ocr.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.iflytek.cosmo.ocr.common.PageResult;
import com.iflytek.cosmo.ocr.common.enums.FeedbackDeleteEnum;
import com.iflytek.cosmo.ocr.common.enums.FeedbackReplyEnum;
import com.iflytek.cosmo.ocr.dao.FeedbackDao;
import com.iflytek.cosmo.ocr.model.entity.FeedbackEntity;
import com.iflytek.cosmo.ocr.service.FeedbackService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {
    /**
     * 日志记录
     */
    private Logger logger = LoggerFactory.getLogger(FeedbackServiceImpl.class);

    @Autowired
    private FeedbackDao feedbackDao;

    @Override
    public Boolean submitFeedback(FeedbackEntity feedbackEntity)
    {
        try {
            feedbackEntity.setCreateTime(new Date());
            feedbackEntity.setCreatorId(feedbackEntity.getUserId());
            feedbackEntity.setOriginId(0);
            feedbackEntity.setReplyCount(0);
            feedbackEntity.setIsDelete(0);
            feedbackDao.insert(feedbackEntity);
            return feedbackEntity.getId() != null;

        }catch (Exception ex){
            logger.error("提交失败" + feedbackEntity.getTitle() + ex.getMessage());
            return false;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean submitReply(FeedbackEntity feedbackEntity) {
        try {
            feedbackEntity.setCreateTime(new Date());
            feedbackEntity.setCreatorId(feedbackEntity.getUserId());
            feedbackEntity.setIsDelete(0);
            feedbackDao.insert(feedbackEntity);

            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",feedbackEntity.getOriginId());
            criteria.andEqualTo("originId",0);
            FeedbackEntity feedbackParent = new FeedbackEntity();
            feedbackParent.setUpdateTime(new Date());
            List<FeedbackEntity> feedbackEntities = feedbackDao.selectByExample(example);
            if(CollectionUtils.isNotEmpty(feedbackEntities))
            {
                feedbackParent.setReplyCount(feedbackEntities.get(0).getReplyCount() + 1);
            }
            int i = feedbackDao.updateByExampleSelective(feedbackParent,example);
            return i != 0;

        }catch (Exception ex){
            logger.error("提交回复信息失败" + feedbackEntity.getTitle() + ex.getMessage());
            throw ex;
        }
    }

    @Override
    public List<FeedbackEntity> getComments(String userId) {
        try {
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("userId",userId);
            criteria.andEqualTo("isDelete",0);
            criteria.andEqualTo("originId",0);
            return feedbackDao.selectByExample(example);

        }catch (Exception ex){
            logger.error("获取评论失败" + ex.toString());
            return null;
        }
    }

    @Override
    public List<FeedbackEntity> getReply(Integer originId) {
        try {
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("originId",originId);
            criteria.andEqualTo("isDelete",0);
            return feedbackDao.selectByExample(example);

        }catch (Exception ex){
            logger.error("列表删除失败"+ ex.getMessage());
            return null;
        }
    }

    @Override
    public Boolean deleteFeedback(Integer id) {
        try {
            FeedbackEntity feedbackEntity = new FeedbackEntity();
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",id);
            criteria.orEqualTo("originId",id);
            criteria.andEqualTo("isDelete",0);
            feedbackEntity.setIsDelete(1);
            feedbackEntity.setUpdateTime(new Date());
            int i = feedbackDao.updateByExampleSelective(feedbackEntity,example);
            return i != 0;
        }catch (Exception ex){
            logger.error("回复评论失败"+ ex.getMessage());
            return  false;
        }
    }

    @Override
    public Boolean deleteReoly(Integer id) {
        try {
            FeedbackEntity feedbackEntity = new FeedbackEntity();
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id",id);
            criteria.andEqualTo("isDelete",0);
            feedbackEntity.setIsDelete(1);
            feedbackEntity.setUpdateTime(new Date());
            int i = feedbackDao.updateByExampleSelective(feedbackEntity,example);
            return i != 0;
        }catch (Exception ex){
            logger.error("回复删除失败"+ ex.getMessage());
            return  false;
        }
    }

    @Override
    public Boolean deleteReolyList(Integer originId) {
        try {
            FeedbackEntity feedbackEntity = new FeedbackEntity();
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("originId",originId);
            criteria.andEqualTo("isDelete",0);
            feedbackEntity.setIsDelete(1);
            feedbackEntity.setUpdateTime(new Date());
            int i = feedbackDao.updateByExampleSelective(feedbackEntity,example);
            return i != 0;
        }catch (Exception ex){
            logger.error("删除回列表失败" + ex.getMessage());
            return  false;
        }
    }

    @Override
    public PageResult<FeedbackEntity> getFeedbcakList(String userId, String companyName,String name,Integer replyStatus, Integer deleteStatus, Integer page, Integer size) {
        try {
            PageResult<FeedbackEntity> result = new PageResult<>(page, size);
            PageHelper.startPage(page, size);
            Example example = new Example(FeedbackEntity.class);
            example.orderBy("createTime").desc();
            Example.Criteria criteria = example.createCriteria();
            if(StringUtils.isNotEmpty(userId))
                criteria.andLike("userId","%" + userId + "%");
            if(StringUtils.isNotEmpty(companyName))
                criteria.andCondition("JSON_EXTRACT(contact, '$.company') LIKE \"%" + companyName + "%\"");
            if(StringUtils.isNotEmpty(name))
                criteria.andCondition("JSON_EXTRACT(contact, '$.feedbackPerson') LIKE \"%" + name + "%\"");
            if(deleteStatus == FeedbackDeleteEnum.DELETE_YES.getCode())
                criteria.andEqualTo("isDelete",1);
            else if(deleteStatus == FeedbackDeleteEnum.DELETE_NO.getCode())
                criteria.andEqualTo("isDelete",0);
            if(replyStatus == FeedbackReplyEnum.REPLY_YES.getCode())
                criteria.andNotEqualTo("replyCount",0);
            else if(replyStatus == FeedbackReplyEnum.REPLY_NO.getCode())
                criteria.andEqualTo("replyCount",0);
            criteria.andEqualTo("originId",0);
            List<FeedbackEntity> feedbackEntities = feedbackDao.selectByExample(example);
            result.setTotal(((Page) feedbackEntities).getTotal());
            result.setData(feedbackEntities);
            return result;
        }catch (Exception ex){
            logger.error("获取反馈列表"+ ex.getMessage());
            return new PageResult<>(page, size);
        }
    }

    @Override
    public List<FeedbackEntity> getReplyList(List<Integer> feedbacksId) {
        try {
            Example example = new Example(FeedbackEntity.class);
            Example.Criteria criteria = example.createCriteria();
            //criteria.andEqualTo("isDelete",0);
            criteria.andIn("originId",feedbacksId);
            return feedbackDao.selectByExample(example);

        }catch (Exception ex){
            logger.error("获取回复列表"+ ex.getMessage());
            return null;
        }
    }
}
